<?php
class VoteAction extends BaseAction
{
    function _initialize()
    {
        $this->assign('nav', 2);
        parent::_initialize();
    }

    public function index()
    {
        $VoteModel = D('Vote');
        $allCount = $VoteModel->count();
        $Page = new Page($allCount, 15);
        $votes = $VoteModel->getList(null, $Page->firstRow . ',' . $Page->listRows);
        $this->assign('votes', $votes);
        $this->assign('page', $Page->show());
        $this->display();
    }

    public function add()
    {
        if ($this->isPost()) {
            $data = $this->getPostData();
            $this->createData('Vote', $data);
        }
        $this->display();
    }

    public function update()
    {
        $this->display();
    }

    /**
     * 获取投票详细
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function detail()
    {
        $itemid = intval($this->_get('id'));
        $vote = M('Vote')->find($itemid);
        if (empty($vote)) {
            $this->error('投票信息不对！');
        }
        //获取投票信息
        $map = array();
        $map['itemid'] = $itemid;
        $VrModel = D('VoteRecord');
        $rCount = $VrModel->getWhereCount($map);
        if ($rCount) {
            $Page = $this->getPage($rCount);
            $vrs = $VrModel->getList($map, $Page->firstRow . ',' . $Page->listRows);
            $this->assign('vrs', $vrs);
            $this->assign('page', $Page->show());
        }
        $this->assign('v', $vote);
        $this->display();
    }

    /**
     * 邀请投票人
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function push()
    {
        $itemid = intval($this->_get('id'));
        $vote = M('Vote')->field('itemid,title,email')->find($itemid);
        if (empty($vote)) {
            $this->error('投票信息不对！');
        }
        if ($this->isPost()) {
            $data = $_POST; //转换 POST 数据
            if ($data['email'] == $vote['email']) {
                $this->error('发布者不能参与投票！');
            }
            $map = array();
            $VrModel = D('VoteRecord');
            //查看是否已经要求过啦
            $map['itemid'] = $itemid;
            $map['email'] = $data['email'];
            $count = $VrModel->where($map)->count();
            $rs = false;
            if (empty($count)) {
                //记录投票信息
                $vr = array();
                $vr['itemid'] = $itemid;
                $vr['email'] = $data['email'];
                $rs = $VrModel->add($vr);
                unset($vr);
            } else {
                $rs = true;
            }
        }
        $this->assign('v', $vote);
        $this->display();
    }

    /**
     * 获取提交数据
     * @param bool $isEdit
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getPostData($isEdit = false)
    {
        $data = $_POST;
        $startTime = $data['start_time'];
        $endTime = $data['end_time'];
        if (empty($startTime)) {
            $this->error('开始时间不能为空！');
        }
        //时间跟格式化
        $startTime = strtotime($startTime);
        $endTime = strtotime($endTime);
        if (time() > $endTime) {
            $this->error('结束时间必须大于当前时间！');
        }
        if ($startTime > $endTime) {
            $this->error('开始时间不能大于结束时间！');
        }
        $data['start_time'] = $startTime;
        $data['end_time'] = $endTime;
        //主题判断至少要有两个不为空
        $isNoEemptyCount = 0;
        for ($i = 1; $i <= 10; $i++) {
            if (!empty($data['s' . $i])) {
                $isNoEemptyCount++;
            }
        }
        if ($isNoEemptyCount < 2) {
            $this->error('至少应该有2个主题！');
        }
        return $data;
    }

    /**
     * 一键给没有投票发一封邮件
     */
    public function sendAll()
    {
        //获取没有发邮件的用户 并且 类型为1 的投票
        $rss = M()->query('SELECT m_vote.itemid,m_vote.title,m_vote_record.email
            FROM
            m_vote
            INNER JOIN m_vote_record ON m_vote.itemid = m_vote_record.itemid
            WHERE
            m_vote.type = 1 AND
            m_vote_record.`status` = 1');
        //合并邮件
        $users = array();
        foreach ($rss as $key => $r) {
            $email = $r['email'];
            $key = encrypt($email . '|' . $r['itemid']);

            $body['title'] = $r['title'];
            $body['activation_url'] = SITE_URL . U('/vote/' . $r['itemid'] . '/' . $key);
            $users[$email][] = $body;
        }
        SD($users);
        //            if ($rs) {
//                //发送邮件 邮件|itemid
//                $key = encrypt($data['email'] . '|' . $itemid);
//                //邮件发送成功进行投票对应信息添加
//                $body = array();
//                $title = array();
//                $body['email'] = $data['email'];
//                $body['activation_url'] = SITE_URL . U('/vote/' . $itemid . '/' . $key);
//                $title['title'] = $vote['title'];
//                $rs = $this->sendMail($data['email'], $title, $body, 'push');
//                if ($rs) {
//                    $this->success('邮件发送成功！');
//                } else {
//                    $this->error('邮件发送失败！');
//                }
//            } else {
//                $this->error('邮件没有能正常发送！');
//            }
        //$rs = $this->sendMail($data['email'], $title, $body, 'pushs');
        //if ($rs) {
        //    $this->success('邮件发送成功！');
        //} else {
        //    $this->error('邮件发送失败！');
        //}
    }
}

?>